import java.io.File;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 
 * @author Peter & Daniel
 *
 */
public class Response {
	
	/**
	 * 
	 * @param request (Beinhaltet Pfad zu angeforderter Datei)
	 * @param out1 (OutputStream zur Ausgabe)
	 */
	private void sendFile(Request request, OutputStream out1){
		PrintWriter out = new PrintWriter(out1);
		out.println(request.httpVersion + " 200 OK");
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.US);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		String ausgabe = sdf.format(new Date());
		out.println("Date: " + ausgabe + " GMT");
		out.println("Content-Type: " + MimeType.getMimeType(request.processedPath));
		out.println("Content-Length: " + new ResponseFile().getFileLength(request.processedPath));
		out.println("");
		out.flush();
		if (!request.method.equals("HEAD")) new ResponseFile().sendFile(request.processedPath, out1);
	}
	
	/**
	 * 
	 * @param request (Beinhaltet Pfad zu angeforderter Datei) 
	 * @param out (OutputStream zur Ausgabe)
	 */
	private void sendDirectoryList(Request request, PrintWriter out){
		String responseString;
		responseString = new ResponseFile().listDirectory(new File(request.processedPath));
		out.println(request.httpVersion + " 200 OK");
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.US);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		String ausgabe = sdf.format(new Date());
		out.println("Date: " + ausgabe + " GMT");
		out.println("Content-Type: text/html");
		out.println("Content-Length: " + responseString.length());
		out.println("");
		if (!request.method.equals("HEAD")) out.println(responseString);
		out.flush();
	}
	
	/**
	 * 
	 * @param request (Beinhaltet Pfad zu angeforderter Datei)
	 * @param ps (OutputStream zur Ausgabe)
	 */
    void send404(Request request, PrintWriter ps) {
    	String send = "<html><title>404 Not Found</title></head><body><h1>Not Found</h1>The requested URL " + request.path + " was not found on this server.<br /><hr /><i>Ehm & Bollinger/0.1 (Windows) Server at Port "+Config.conf.getProperty("port")+"</i></body></html>";
    	ps.println("HTTP/1.1 404 Not Found");
    	SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.US);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		String ausgabe = sdf.format(new Date());
		ps.println("Date: " + ausgabe + " GMT");
        ps.println("Content-Type: text/html");
        ps.println("Content-Length: " + send.length());
        ps.println("");
        if (!request.method.equals("HEAD")) ps.println(send);
        ps.flush();
    }
    
    /**
     * 
	 * @param request (Beinhaltet Pfad zu angeforderter Datei)
	 * @param ps (OutputStream zur Ausgabe)
     */
    public void send501(Request request, PrintWriter ps) {
    	String send = "<html><title>501 Not Implemented</title></head><body><h1>Not Implemented</h1><hr /><i>Ehm & Bollinger/0.1 (Windows) Server at Port "+Config.conf.getProperty("port")+"</i></body></html>";
    	ps.println("HTTP/1.1 501 Not Implemented");
    	SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.US);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		String ausgabe = sdf.format(new Date());
		ps.println("Date: " + ausgabe + " GMT");
        ps.println("Content-Type: text/html");
        ps.println("Content-Length: " + send.length());
        ps.println("");
        ps.println(send);
        ps.flush();
    }
    
    /**
     * 
	 * @param request (Beinhaltet Pfad zu angeforderter Datei)
	 * @param ps (OutputStream zur Ausgabe)
     */
    public void send400(Request request, PrintWriter ps) {
    	String send = "<html><title>400 Bad Request</title></head><body><h1>Bad Request</h1><hr /><i>Ehm & Bollinger/0.1 (Windows) Server at Port "+Config.conf.getProperty("port")+"</i></body></html>";
    	ps.println("HTTP/1.1 400 Bad Request");
    	SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.US);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
		String ausgabe = sdf.format(new Date());
		ps.println("Date: " + ausgabe + " GMT");
        ps.println("Content-Type: text/html");
        ps.println("Content-Length: " + send.length());
        ps.println("");
        ps.println(send);
        ps.flush();
    }
	
    /**
     * 
     * @param request (Beinhaltet Pfad zu angeforderter Datei)
     * @param out1 (OutputStream zur Ausgabe)
     */
	public void sendResponse(Request request, OutputStream out1){
		PrintWriter out = new PrintWriter(out1);
		ResponseFile responseFile = new ResponseFile();
		long fileLength = responseFile.getFileLength(request.processedPath);
		if (request.httpVersion.equals("HTTP/1.1")){
			out.println(request.httpVersion + " 100 Continue");
			out.println("");
			out.flush();
		}
		if (fileLength==0){
			send404(request,out);
		} else if (fileLength==-1){
			if (responseFile.getFileLength(request.processedPath + "index.html")==0){
				sendDirectoryList(request, out);
			} else {
				request.processedPath += "index.html";
				sendFile(request, out1);
			}
		} else {
			sendFile(request, out1);
		}

	}
}
